<template>
  <div class="app-container">
    <el-row class="header">
      <el-button
        type="success"
        :icon="DocumentAdd"
        @click="handlerDialogValue(null)"
        >新增菜单</el-button
      >
    </el-row>

    <el-table
      :data="tableData"
      row-key="id"
      stripe
      style="width: 100%; margin-bottom: 20px"
      border
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column prop="name" label="菜单名称" width="200" />
      <el-table-column prop="icon" label="图标" width="70" align="center">
        <template v-slot="scope">
          <el-icon><svg-icon :icon="scope.row.icon" /></el-icon>
        </template>
      </el-table-column>
      <el-table-column prop="orderNum" label="排序" width="70" align="center" />
      <el-table-column prop="perms" label="权限标识" width="200" />
      <el-table-column prop="path" label="前端路由" width="180" />
      <el-table-column prop="component" label="index组件路径" width="180" />
      <el-table-column
        prop="menuType"
        label="菜单类型"
        width="120"
        align="center"
      >
        <template v-slot="scope">
          <el-tag
            size="small"
            v-if="scope.row.menuType === 'M'"
            type="danger"
            effect="dark"
            >目录</el-tag
          >
          <el-tag
            size="small"
            v-else-if="scope.row.menuType === 'C'"
            type="success"
            effect="dark"
            >菜单</el-tag
          >
          <el-tag
            size="small"
            v-else-if="scope.row.menuType === 'F'"
            type="warning"
            effect="dark"
            >按钮</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" />
      <el-table-column
        prop="action"
        label="操作"
        width="400"
        fixed="right"
        align="center"
      >
        <template v-slot="scope">
          <el-button
            type="primary"
            :icon="Edit"
            @click="handlerDialogValue(scope.row.id)"
          />
          <el-popconfirm
            title="您确定要删除这条记录吗？"
            @confirm="handleDelete(scope.row.id)"
          >
            <template #reference>
              <el-button type="danger" :icon="Delete" />
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <Dialog
    v-model="dialogFormVisible"
    :tableData="tableData"
    :dialogFormVisible="dialogFormVisible"
    :id="id"
    :dialogTitle="dialogTitle"
    @initMenuList="initMenuList"
  />
</template>

<script setup>
import { ref, reactive } from "vue";
import requestUtil, { getServerUrl } from "@/util/request";
import {
  Search,
  Delete,
  DocumentAdd,
  Edit,
  Tools,
  RefreshRight,
} from "@element-plus/icons-vue";
import Dialog from "./components/Dialog";
import { ElMessage, ElMessageBox } from "element-plus";

const tableData = ref([]);
const dialogFormVisible = ref(false);
const dialogTitle = ref("");
const id = ref("");

//初始化tableData
const initMenuList = async () => {
  const res = await requestUtil.get("menu/treeList");
  tableData.value = res.data.treeMenu;
};
initMenuList();

//修改
let handlerDialogValue = (menuId) => {
  // console.log(userId);
  if (menuId) {
    id.value = menuId;
    dialogTitle.value = "角色修改";
  } else {
    id.value = "";
    dialogTitle.value = "角色新增";
  }
  dialogFormVisible.value = true;
};

//删除
const handleDelete = async (id) => {
  const res = await requestUtil.post("menu/delete/" + id);
  if (res.code == 20000) {
    ElMessage({
      type: "success",
      message: res.message,
    });
    initMenuList();
  } else {
    ElMessage({
      type: "error",
      message: res.message,
    });
  }
};
</script>
<style lang="scss" scoped>
.header {
  margin-bottom: 20px;
  .el-col {
    display: flex;
    .el-button {
      margin: 5px;
    }
  }
}
.app-container {
  padding: 0px;
  .el-pagination {
    display: flex;
    justify-content: flex-end;
  }
}
</style>